<?php
namespace App\Services;

use App\Models\OrderRefund;
use App\Models\RefundGoods;
use App\Services\CashierService;
use App\Models\Address;
use App\Models\Order;
use App\Models\OrderGoods;
use Illuminate\Support\Facades\DB;
/**
 *
 * 退货款服务层
 *
 * @author shengl@maqu.im
 *
 */
class RefundService extends BaseService {

    /**
     * 获取订单列表
     *
     * @param $user_id 用户ID
     * @param String $nav 导航
     * @param int $firstrow         第一行
     * @param int $pagesize         每页条数
     * @return mixed
     */
    public function getRefundList($user_id,$firstrow=0,$pagesize=10){
        //获取订单列表数据
        $refund_orders = OrderRefund::where('applier',$user_id)
            ->where('refund_status','<>',REFUND_CANCELED)
            ->orderBy('apply_date','desc')->skip($firstrow)->take($pagesize)
            ->with(['refundGoods','order','order.orderExtm','refundGoods.orderGoods'])->get()->toArray();

        return $refund_orders;
    }

    /**
     * 获取订单商品详情
     *
     * @param $user_id 用户ID
     * @param int $rec_id 订单商品id
     * @return mixed
     */
    public function getOrderGoodsInfo($rec_id){
        //获取订单列表数据
        $query = OrderGoods::where('rec_id',$rec_id);

        $orderGoods = $query->first();

        return $orderGoods;
    }

    /**
     * 申请退款/货
     * 新增或者修改退款/货信息
     * @param $refund_id 退款记录id 第一次添加为0
     * @param $user_id 退款用户id
     * @param $refund_type 退款/货类型
     * @param $refund_reason 退款理由
     * @param $refund_amount 退款金额
     * @param $refund_desc 退款说明
     * @param $orderInfo 需要退款的订单信息
     * @param $orderGoods 退款商品信息
     *
     */
    public function addRefund($refund_id = 0,$user_id,$refund_type,$refund_reason,$refund_amount,$refund_desc,$orderInfo,$orderGoods){
        $orderRefund = OrderRefund::findOrNew($refund_id);

        $orderRefund->order_id = $orderInfo->order_id;
        $orderRefund->refund_type = $refund_type;
        $orderRefund->applier = $user_id;
        $orderRefund->apply_date = gmtime();
        $orderRefund->apply_reason = $refund_reason;
        $orderRefund->receiver = "";
        $orderRefund->receiver_remarks = "";
        $orderRefund->refund_status = REFUND_PENDING;
        $orderRefund->refund_remarks = $refund_desc;
        $orderRefund->refund_fee = $refund_amount;
        $orderRefund->last_time = 0;
        $orderRefund->refund_ship_time = 0;
        $orderRefund->refund_invoice_no = "";

        $orderRefund->save();

        $refundGoods = RefundGoods::where("refund_id",$orderRefund->refund_id)->first();
        if(!$refundGoods){
            $refundGoods = new RefundGoods;
        }
        $refundGoods->refund_id =$orderRefund->refund_id;
        $refundGoods->goods_id = $orderGoods->goods_id;
        $refundGoods->spec_id = $orderGoods->spec_id;
        $refundGoods->quantity = $orderGoods->quantity;
        $refundGoods->sku = $orderGoods->sku;
        $refundGoods->order_goods_id = $orderGoods->rec_id;
        $refundGoods->save();
    }

    /**
     *    获取订单状态相应的文字表述
     *
     *    @param     int $refund_type  退款类型 1->退款 2->退货 0->无
     *    @param     int $refund_status  退款/货状态
     *    @return    string
     */
    public function refundStatus($refund_type=0,$refund_status=0)
    {
        $lang_key = '';
        switch ($refund_type)
        {
            case REFUND_TYPE_REFUND:
                switch ($refund_status)
                {
                    case REFUND_PENDING://等待卖家同意退款
                        $lang_key = 'refund_pending_1';
                        break;
                    case REFUND_REFUNDING:// 退款中
                        $lang_key = 'refund_refunding';
                        break;
                    case REFUND_FINISHED:// 退款成功
                        $lang_key = 'refund_finished_1';
                        break;
                    case REFUND_CANCELED:// 退款已取消

                        break;
                }
                break;
            case REFUND_TYPE_RETURN_GOODS:
                switch ($refund_status)
                {
                    case REFUND_PENDING://等待卖家同意退货
                        $lang_key = 'refund_pending_2';
                        break;
                    case REFUND_ACCEPTED:// 卖家已审核，等待买家发货
                        $lang_key = 'refund_accepted';
                        break;
                    case REFUND_SHIPPED:// 买家已发货
                        $lang_key = 'refund_shipped';
                        break;
                    case REFUND_REFUNDING:// 退款中
                        $lang_key = 'refund_refunding';
                        break;
                    case REFUND_FINISHED:// 退货成功
                        $lang_key = 'refund_finished_2';
                        break;
                    case REFUND_CANCELED:// 退货已取消

                        break;
                }
                break;
            default :;
        }

        $keyvalues = array(
            'refund_pending_1'   => '退款待审核',
            'refund_pending_2'     => '退货待审核',
            'refund_accepted'     => '等待买家发货',
            'refund_shipped'    => '买家已发货',
            'refund_refunding'   => '退款中',
            'refund_finished_1'	=> '退款成功',
            'refund_finished_2'    => '退货成功');

        return $lang_key  ? $keyvalues[$lang_key] : $lang_key;
    }

    /**
     * 添加退款/货订单操作日志
     * @param $refund_id 退款/货订单id
     * @param $user_name 操作者用户名
     * @param $refund_type 退款类型
     * @param $old_status 订单原先状态
     * @param $new_status 操作后的订单撞他
     * @param $remark 操作原因
     * @return null
     */
    public function addOrderRefundLog($refund_id,$user_name,$refund_type,$old_status,$new_status,$remark){
        DB::table('order_refund_log')->insert(
            array(
                'refund_id' => $refund_id,
                'operator'	=> $user_name,
                'refund_status' => $this->refundStatus($refund_type,$old_status),
                'changed_status' => $this->refundStatus($refund_type,$new_status),
                'remark' => $remark,
                'log_time' => gmtime(),
            )
        );
    }
}
